/**  
* Filename		:	DepotDaoImpl.java  
* Description	:   
* Copyright		:   Copyright (c)2010-2011  
* Company		:   
* @author		:   yanglianbo 
* @version		:   1.0  
* Create at		:   Jun 10, 2011 10:23:02 AM  
*  
* Modification History:  
* Date			Author      	Version    	Description  
* ------------------------------------------------------------------  
* Jun 10, 2011     	yanglianbo    	1.0        	1.0 Version  
*/   
package com.zoomlion.example.users.daoimpl;


import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.zoomlion.core.dao.CommonDAO;
import com.zoomlion.core.util.ViewPage;
import com.zoomlion.example.dao.entity.User;
import com.zoomlion.example.users.dao.UserManagerDao;


/**
 * @author 		:	yanglian
 * Description	:
 * Create at 	:  	Jun 10, 2011 10:23:02 AM
 */
@Repository("userManagerDao")
public class UserManagerDaoImpl extends CommonDAO implements UserManagerDao {
	public static final int PAGE_NUM = 20;

	@Override
	public ViewPage getAllUser(User user,ViewPage page) {
		// TODO Auto-generated method stub
		this.logger.info("In UserManagerDaoImplgetAllUser begin--------------------");
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		if(user!=null)
		{

			if(user.getUserName()!=null&&!user.getUserName().equals(""))
				criteria.add(Restrictions.like("userName", "%"+user.getUserName()+"%"));

			if(user.getStroesCode()!=null&&!user.getStroesCode().equals(""))
				criteria.add(Restrictions.like("stroesCode", "%"+user.getStroesCode()+"%"));

		}else
		{
			user=new User();
		}
		int count=this.getTotalSizeForCriteria(criteria);
		page.setTotalSize(count);
		criteria.addOrder(Order.asc(page.getOrderField()==null||page.getOrderField().equals("")?"userId":page.getOrderField()));
		List resultList=this.findByCriteria(criteria, (page.getPageNum()-1)*page.getNumPerPage()+1, page.getNumPerPage());
		page.setPageDataList(resultList);
		this.logger.info("In UserManagerDaoImpl getAllUser end--------------------");
		return page;
	}

	@Override
	public void addAllUser(User user) {
		this.save(user);
	}
}
